package com.meraklisina.latexeditor;

import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.*;
import java.awt.TrayIcon.MessageType;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.meraklisina.latexeditor.wizard.DynamicToolbarWizard;


public class ToolbarTransporter{
	JFileChooser fc;
	static String _operatingSystem = System.getProperty("os.name");
	Language lang = new Language();
	
    private AppConfigReader appConf ;
	
	public ToolbarTransporter(boolean isImport)
	{
		int retVal;
		File folderToCopy;
		File folderCopyTo;
		String ToolbarName;
		
		lang.LoadLanguage(BlueLatexEditor._language);
		
		fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		if(isImport == false)
		{
			fc.setCurrentDirectory(new File("."));
			fc.setDialogTitle(lang.ExportDialogTittle1);
		}
		else
		{
			fc.setDialogTitle(lang.ImportDialogTittle);
		}
		
		retVal = fc.showOpenDialog(null);

		if(retVal == JFileChooser.APPROVE_OPTION) 
		{
			folderToCopy = fc.getSelectedFile();
			if(_operatingSystem.contains("Linux"))
			{
				ToolbarName = folderToCopy.toString().substring(folderToCopy.toString().lastIndexOf("/")+1);
			}
			else
			{
				ToolbarName = folderToCopy.toString().substring(folderToCopy.toString().lastIndexOf("\\")+1);
			}
			ProcessBuilder pbuilder = new ProcessBuilder();
			
			if(isImport)
			{
				if(_operatingSystem.contains("Linux"))
					pbuilder.command("cp", "-rf", folderToCopy.toString(), ".");
				else
					pbuilder.command("xcopy", folderToCopy.toString(), ToolbarName, "/s/i");	
			
				try {
					pbuilder.start();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				JOptionPane.showMessageDialog(null,lang.ToolbarImported); 
				
		        LatexToolBar importedToolbar =  new LatexToolBar(ToolbarName, ToolbarName, false );
		        ToolBarConfigReader reader = new ToolBarConfigReader(ToolbarName);
		        importedToolbar = reader.GenerateButtons(importedToolbar);
		        ToolBarHandler.getHandler().getToolbars().add(importedToolbar);
	            ToolBarHandler.getHandler().serializeToolBars();
			}
		}
		else
		{
			return;
		}
		
		if(isImport == false)
		{
			fc.setDialogTitle(lang.ExportDialogTittle2);
			retVal = fc.showOpenDialog(null);
			
			if(retVal == JFileChooser.APPROVE_OPTION) 
			{
				folderCopyTo = fc.getSelectedFile();
				ToolbarName = folderToCopy.toString().substring(folderToCopy.toString().lastIndexOf("\\")+1);
				System.out.println(folderCopyTo.toString());
				ProcessBuilder pbuilder = new ProcessBuilder();
				
				if(_operatingSystem.contains("Linux"))
					pbuilder.command("cp", "-rf", folderToCopy.toString(), folderCopyTo.toString());
				else
					pbuilder.command("xcopy", folderToCopy.toString(), folderCopyTo.toString() + ToolbarName, "/s/i");	
			
				try {
					pbuilder.start();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			else
			{
				return;
			}
		}

	}
}
